package  
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.FlxExtendedSprite;
	import org.flixel.plugin.photonstorm.FlxMouseControl;
	/**
	 * ...
	 * @author Lachlan McInnes
	 */
	
	public class LightPuzzle extends ExtendedFlxState
	{
		private const xTileNumber:uint = 20;
		private const yTileNumber:uint = 20;
		private const gridSpace:uint = 1;
		private const squareTiles:Boolean = true; //false: sets tile size to max for screen size, true: centers square tiles
		
		private var particles:FlxGroup;
		private var baseGrid:FlxGroup;
		private var gridWidth:uint;
		private var gridHeight:uint;
		private var xScreenBuffer:uint = 30;
		private var yScreenBuffer:uint = xScreenBuffer;
		
		private var beamWidth:uint = 5;
		private var redBeam:FlxSprite;
		private var yellowBeam:FlxSprite;
		private var blueBeam:FlxSprite;
		
		public function LightPuzzle() 
		{
			baseGrid = new FlxGroup(xTileNumber * yTileNumber);
			
			gridWidth = (FlxG.width - xScreenBuffer * 2) / xTileNumber;
			gridHeight = (FlxG.height - yScreenBuffer * 2) / yTileNumber; 
			
			if (squareTiles) //makes the grid tiles squares if sqaureTiles is true by choosing the smaller value and setting both to it
			{
				if (gridHeight < gridWidth)
				{
					xScreenBuffer += ((gridWidth - gridHeight) * xTileNumber) / 2;
					gridWidth = gridHeight;
				}
				else if (gridWidth < gridHeight)
				{
					yScreenBuffer += ((gridWidth - gridHeight) * yTileNumber) / 2;
					gridHeight = gridWidth;
				}
			}
			
			for (var i:int = 0; i < baseGrid.maxSize; i++)
			{
				var temp:FlxExtendedSprite;
				temp = new FlxExtendedSprite((i % xTileNumber) * gridWidth + xScreenBuffer, ((uint) (i / xTileNumber)) * gridHeight + yScreenBuffer);
				temp.makeGraphic(gridWidth - gridSpace, gridHeight - gridSpace, Color.Grey);
				temp.boundsRect = new FlxRect(0, 0, FlxG.width, FlxG.height);
				baseGrid.add(temp);
			}
			
			add(baseGrid);
			
			redBeam = new FlxSprite(xScreenBuffer + (gridWidth * xTileNumber / 2) + (gridWidth * 0 / 3) + (beamWidth / 2), yScreenBuffer);
			redBeam.makeGraphic(beamWidth, gridHeight * xTileNumber, Color.Red);
			add(redBeam);
			yellowBeam = new FlxSprite(xScreenBuffer + (gridWidth * xTileNumber / 2) + (gridWidth * 1 / 3) + (beamWidth / 2), yScreenBuffer);
			yellowBeam.makeGraphic(beamWidth, gridHeight * xTileNumber, Color.Yellow);
			add(yellowBeam);
			blueBeam = new FlxSprite(xScreenBuffer + (gridWidth * xTileNumber / 2) + (gridWidth * 2 / 3) + (beamWidth / 2), yScreenBuffer);
			blueBeam.makeGraphic(beamWidth, gridHeight * xTileNumber, Color.Blue);
			add(blueBeam);
		}
		
		override public function update():void 
		{
			super.update();
			
			if (FlxMouseControl.dragTarget != null)
			{
				var temp:FlxExtendedSprite;
				temp = FlxMouseControl.dragTarget;
				baseGrid.remove(FlxMouseControl.dragTarget, true);
				baseGrid.add(temp);
			}
		}	
	}
}